Skip to content

[FLINK-39284][table] CREATE OR ALTER MATERIALIZED TABLE doesn't respect schema of existing table#27807

Open
snuyanzin wants to merge 7 commits intoapache:masterfrom
snuyanzin:flink39284
Open

[FLINK-39284][table] CREATE OR ALTER MATERIALIZED TABLE doesn't respect schema of existing table#27807
snuyanzin wants to merge 7 commits intoapache:masterfrom
snuyanzin:flink39284

Conversation

@snuyanzin
Copy link
Contributor

What is the purpose of the change

A number of cases like

CREATE MATERIALIZED TABLE mt1 (
   id INT, t TIMESTAMP_LTZ(3)
)
AS SELECT 1 as id, current_timestamp as t

and then

CREATE OR ALTER MATERIALIZED TABLE mt1 (
   id INT, t TIMESTAMP_LTZ(3),
   WATERMARK FOR t as current_timestamp - INTERVAL '5' SECOND
)
AS SELECT 1 as id, current_timestamp as t

it will ignore watermark (all cases like create/modify/drop)
same for constraint
also similar issue for column comments

Brief change log

converter and tests

Verifying this change

new tests introduced

Does this pull request potentially affect one of the following parts:

  • Dependencies (does it add or upgrade a dependency): ( no)
  • The public API, i.e., is any changed class annotated with @Public(Evolving): ( no)
  • The serializers: ( no)
  • The runtime per-record code paths (performance sensitive): (no)
  • Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: (no )
  • The S3 file system connector: (no )

Documentation

  • Does this pull request introduce a new feature? (no)
  • If yes, how is the feature documented? (not applicable)

@flinkbot
Copy link
Collaborator

flinkbot commented Mar 22, 2026

CI report:

Bot commands The @flinkbot bot supports the following commands:
  • @flinkbot run azure re-run the last Azure build

Comment on lines +306 to +309
if (!oldColumn.getName().equals(newColumn.getName())
|| !LogicalTypeCasts.supportsImplicitCast(
oldColumn.getDataType().getLogicalType(),
newColumn.getDataType().getLogicalType())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens here if we change we change the type of the column here? From CHAR(1) -> CHAR(4)

Comment on lines +100 to +102
modifyColumnComment.getNewComment() == null
? ""
: modifyColumnComment.getNewComment());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we avoid MODIFY COMMENT if the comment is null, instead of returning:

MODIFY 'id' COMMENT ''

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, this type modification means it is removed
since there is no a dedicated table change for that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the new test setup. I noticed that some cases are not tests:

  • Adding a constraint, for the case that oldConstraint == null
  • Unchanged table (zero chagnes)
  • It would be interesting to have a test that has a schema with only watermark/constraint, no regular columns

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be interesting to have a test that has a schema with only watermark/constraint, no regular columns

what do you mean by that?

if there is no explicit columns in schema they will be derived from query and there are already such tests here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If other tests are covering it, I am fine with it. Thanks for the explanation!

Copy link
Contributor

@AHeise AHeise left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for all the fixes. Had some minor things.

assertThat(operation.asSummaryString())
.isEqualTo(
"CREATE OR ALTER MATERIALIZED TABLE builtin.default.mt\n"
+ " DROP CONSTRAINT ct1,\n"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These extra space on the first modification look unintended. Can you please double-check?

|| !LogicalTypeCasts.supportsImplicitCast(
oldColumn.getDataType().getLogicalType(),
newColumn.getDataType().getLogicalType())) {
throw new ValidationException(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move the exception also to the operation and just issue a TableChange.ModifyPhysicalColumnType?

+ "Column mismatch at position %d: Original column is [%s], but new column is [%s].",
i + 1, oldColumn, newColumn));
}
if (!Objects.equals(oldColumn.getComment(), newColumn.getComment())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we often treat comment = '' == comment = null, shall we normalize here? Wrap comments in some StringUtil.emptyIfNull.

" MODIFY %s COMMENT '%s'",
EncodingUtils.escapeIdentifier(modifyColumnComment.getNewColumn().getName()),
modifyColumnComment.getNewComment());
modifyColumnComment.getNewComment() == null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2-3 commits before, we already had some changes to the comment logic. Can you double-check if the commits are correctly cut?

"CREATE MATERIALIZED TABLE mt1 (\n"
+ " id INT, t TIMESTAMP_LTZ(3),\n"
+ " WATERMARK FOR t as current_timestamp - INTERVAL '5' SECOND"
+ " WATERMARK FOR t as current_timestamp - INTERVAL '5' SECOND\n"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change should probably go into previous commit.

}

@Test
void testCreateOrAlterMaterializedTableWithNewConstraint()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please squash into respective commit.

@github-actions github-actions bot added the community-reviewed PR has been reviewed by the community. label Mar 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-reviewed PR has been reviewed by the community.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants